package LeetCode;

import java.util.ArrayList;

public class LC_004_MedianOfTwoSortedArrays {
    public static void main(String[] args) {

    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int i = 0, j = 0;
        ArrayList<Integer> arr = new ArrayList<>();
        //归并
        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] > nums2[j])
                arr.add(nums2[j++]);
            else
                arr.add(nums1[i++]);
        }
        while (i < nums1.length)
            arr.add(nums1[i++]);

        while (j < nums2.length)
            arr.add(nums2[j++]);

        //计算
        int n = arr.size();
        if (n % 2 == 0)
            return (arr.get(n / 2) + arr.get(n / 2 - 1)) / 2.0;
        else
            return arr.get(n / 2);
    }
}
